<?php

namespace App\Console\Commands;

use App\Models\BookIsbn;
use App\Models\Goods;
use Illuminate\Console\Command;

class MigrateIsbnCommand extends Command {
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'system:migrate {num?}';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Command description';

	protected $limit = 10000;

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct() {
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle() {
		$num = $this->argument('num');

		// $offset = $this->offset($num);
		$books = BookIsbn::all();
		$a = 0;
		foreach ($books as $book) {
			print $book->id . ":" . $book->title . " : " . $book->isbn13 . "\n";
			$a++;
			$count = Goods::where('isbn', $book->isbn13)->orWhere('isbn', $book->isbn10)->get()->count();
			var_dump($count);
			$book->sell_count = $count;
			$res = $book->save();
			print $a . "\n";
		}

	}

	protected function offset($num) {
		return $offset = ($num - 1) * $this->limit;
	}
}
